const singleton = (function () {
  // 私有变量和方法
  let instance;

  function Init(name, age) {
    this.name = name;
    this.age = age;
  }
  Init.prototype.greet = function () {
    console.log(
      `Hello, my name is ${this.name} and I am ${this.age} years old.`
    );
  };

  return {
    getInstance: function (name, age) {
      if (!instance) {
        instance = new Init(name, age);
      }
      return instance;
    },
  };
})();

// 使用单例
const instance1 = singleton.getInstance("张三", 11);
const instance2 = singleton.getInstance("李四", 22);
console.log(instance1, instance2);

console.log(instance1 === instance2); // true，同一个实例
